/*
 * Created: Author: Aléx Carvalho / Date: May 16, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 01:31:49 -0300 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.editors;

import com.cpm.view.zk.layout.Margin;
import com.cpm.view.zk.layout.Constraints;
import com.cpm.view.zk.layout.TableLayout;
import com.cpm.view.Element;
import com.cpm.view.zk.field.AbstractInputField;
import com.cpm.view.zk.field.LabelField;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zul.Groupbox;

public class EditorPanel<T> implements Element<HtmlBasedComponent> {

    protected T clone;

//    private final MapList<Integer, AbstractInputField> inputFields = new MapList<>();
    private final Groupbox groupbox = new Groupbox();
    private final TableLayout tableLayout = new TableLayout();

    private final String COLSPAN_ATTR = "colspanAttr";

//    private int countRequiredFields = 0;
    public EditorPanel(String title) {
        groupbox.setTitle(title);
    }

    protected void validateInput() {
    }

    protected void updateSource() {
    }

    protected void updatePanel() {
    }

    public final void setSource(T source) {
        this.clone = source;
        updatePanel();
    }
    

    @Override
    public HtmlBasedComponent getElement() {
        groupbox.getChildren().clear();
        groupbox.appendChild(tableLayout.getElement());
        return groupbox;
    }

    public final void addInputField(final AbstractInputField field, final int rowIndex) {
        addInputField(field, rowIndex, null);
    }
    public final void addInputField(final AbstractInputField field, final int rowIndex, Constraints constraints) {
        if (field.getLabel() != null) {
            LabelField labelField = new LabelField(field.getLabel());
            
            tableLayout.addComponent(labelField.getElement(), rowIndex, new Constraints(1, 1, Constraints.Anchor.NORTHEAST, new Margin(8, 0, 0, 0)));
        }

        tableLayout.addComponent(field.getElement(), rowIndex, constraints);
    }
}
